//
//  BookViewControllerC.m
//  Unicorn
//
//  Created by xalo on 16/4/30.
//  Copyright © 2016年 bhj_jun. All rights reserved.
//

#import "BookViewControllerC.h"
#import "BookCCell.h"
#import "BookCModel.h"

#define kURLc @"http://law.anywebapps.com/law/api.php"
  //参数  (tasks=lawsInfo&id=791&language=0)

@interface BookViewControllerC ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,retain)UITableView *tableView;

@property (nonatomic,retain)NSMutableArray *dataSourceArr;

@end

@implementation BookViewControllerC

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.view addSubview:self.tableView];
   
    //数据请求
    [self requestDataWithUrl:kURLc];
    
    //    cell的高度自适应
    self.tableView.estimatedRowHeight  = 300;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    //注册单元格
    [self.tableView registerNib:[UINib nibWithNibName:@"BookCCell" bundle:nil] forCellReuseIdentifier:@"BookCCell"];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - - 懒加载
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}

-(NSMutableArray *)dataSourceArr
{
    if (!_dataSourceArr) {
        _dataSourceArr=[NSMutableArray array];
    }
    return _dataSourceArr;
}


#pragma mark - — 数据请求
-(void)requestDataWithUrl:(NSString *)url
{
    [SingleExE postFetchDataWithUrlString:kURLc paramenters:@{@"tasks":@"lawsInfo",@"id":self.passid,@"language":@(0)} success:^(id data) {
        
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data
                                                        options:NSJSONReadingAllowFragments error:nil];
        NSDictionary *dicB = dic[@"tasks"];
        
  
        BookCModel *model = [[BookCModel alloc]init];
        [model setValuesForKeysWithDictionary:dicB];
        [self.dataSourceArr addObject:model];
       
//NSLog(@"bookCC--数据请求成功--%@",dic);
        
        [self.tableView reloadData];
    } fail:^{
        
        NSLog(@"bookCC--数据请求失败--");
    } view:self.view];
    
    
}

#pragma mark - - tableView代理、
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BookCCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BookCCell" forIndexPath:indexPath];
    
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    BookCModel *model = self.dataSourceArr[indexPath.row];
    
    [cell setCellWithModel:model];

    return cell;
    
}
//
//-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    BookCModel *model = self.dataSourceArr[indexPath.row];
//    
//    CGRect rect= [model.content boundingRectWithSize:CGSizeMake(CGRectGetWidth(self.tableView.frame), MAXFLOAT) options: NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Symbol" size:12]} context:nil];
//    return CGRectGetHeight(rect)+40;
//}





@end
